package com.cn.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cn.bean.Account;
import com.cn.bean.City;
import com.cn.bean.Community;
import com.cn.bean.EnumState;
import com.cn.bean.Provider;
import com.cn.bean.Provider_servicecatalog;
import com.cn.bean.Servicecatalog;
import com.cn.bean.Servicetype;
import com.cn.dao.AccountDAO;
import com.cn.dao.CityDAO;
import com.cn.dao.CommunityDAO;
import com.cn.dao.OrderDAO;
import com.cn.dao.ProviderDAO;
import com.cn.entity.ProviderEntity;
import com.cn.service.ProviderService;
import com.opensymphony.xwork2.ActionContext;

public class ProviderServiceImpl implements ProviderService {

	private ProviderDAO providerDao = null;
	private OrderDAO orderDao = null;
	private CityDAO cityDao = null;
	private AccountDAO accountDao = null;
	private CommunityDAO communityDao = null;
	HttpServletRequest request;

	public CommunityDAO getCommunityDao() {
		return communityDao;
	}

	public void setCommunityDao(CommunityDAO communityDao) {
		this.communityDao = communityDao;
	}

	public AccountDAO getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDAO accountDao) {
		this.accountDao = accountDao;
	}

	public OrderDAO getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDAO orderDao) {
		this.orderDao = orderDao;
	}

	public CityDAO getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDAO cityDao) {
		this.cityDao = cityDao;
	}

	public ProviderDAO getProviderDao() {
		return providerDao;
	}

	public void setProviderDao(ProviderDAO providerDao) {
		this.providerDao = providerDao;
	}

	/**
	 * 获取某st的供应商列表
	 * 
	 * @return
	 */
	public List<ProviderEntity> getSTProviders() throws Exception {
		request = ServletActionContext.getRequest();
		request.setCharacterEncoding("utf-8");
		int st_id = Integer.parseInt(request.getParameter("st_id"));
		List<Object> list = providerDao.findByST(st_id);
		List<ProviderEntity> list1 = new ArrayList<ProviderEntity>();
		Provider p;
		for (int i = 0; i < list.size(); i++) {
			p = (Provider) list.get(i);
			ProviderEntity pe = new ProviderEntity(p.getId(), p.getName(), p.getRank(), p.getPhone());
			list1.add(pe);
		}
		return list1;
	}

	/**
	 * 获取某sc的供应商列表
	 * 
	 * @return
	 */
	public List<ProviderEntity> getSCProviders() throws Exception {
		request = ServletActionContext.getRequest();
		request.setCharacterEncoding("utf-8");
		String account = request.getParameter("re_account");
		System.out.println("帐号是" + account);
		Account accountE = (Account) accountDao.findByAccount(account);
		Community community = (Community) communityDao.findById(accountE.getCommunity().getId());
		int cityId = community.getCity().getIndex();
		System.out.println("cityId=" + cityId);
		int sc_id = Integer.parseInt(request.getParameter("sc_id"));
		System.out.println("sc_id=" + sc_id);
		List<Object> list = providerDao.findBySC(sc_id, cityId);
		List<ProviderEntity> list1 = new ArrayList<ProviderEntity>();
		Provider p;
		for (int i = 0; i < list.size(); i++) {
			p = (Provider) list.get(i);
			ProviderEntity pe = new ProviderEntity(p.getId(), p.getName(), p.getRank(), p.getPhone());
			if (p.getIntroduction() == null) {
				pe.setIntroduction("");
			} else {
				pe.setIntroduction(p.getIntroduction());
			}
			if (p.getPicturepath() == null) {
				pe.setPicturepath("");
			} else {
				String realpath = ServletActionContext.getServletContext().getRealPath("/permit") + "\\";
				String name = p.getPicturepath().replace(realpath, "");
				pe.setPicturepath(name);
			}
			pe.setService("");
			list1.add(pe);
		}
		return list1;
	}

	/**
	 * 获取金牌服务商
	 */
	public List<ProviderEntity> getRecommendProviders() throws Exception {
		List<Object> list = providerDao.listRecommendProviders();
		List<ProviderEntity> list1 = new ArrayList<ProviderEntity>();
		Provider p;
		for (int i = 0; i < list.size(); i++) {
			p = (Provider) list.get(i);
			
			ProviderEntity pe = new ProviderEntity(p.getId(), p.getName(), p.getRank(), p.getPhone());
			if (p.getIntroduction() == null) {
				pe.setIntroduction("");
			} else {
				pe.setIntroduction(p.getIntroduction());
			}
			if (p.getPicturepath() == null) {
				pe.setPicturepath("");
			} else {
				String realpath = ServletActionContext.getServletContext().getRealPath("/permit") + "\\";
				String name = p.getPicturepath().replace(realpath, "");
				pe.setPicturepath(name);
			}
			List<Object> pscList = providerDao.getSCbyPid(p.getId());
			String service="";
			for (int j = 0; j < pscList.size(); j++) {
				Provider_servicecatalog psc = (Provider_servicecatalog) pscList.get(j);
				Servicecatalog sc = psc.getServicecatalog();
				String scname = sc.getName();
				/*Servicetype st=sc.getServicetype();
				String stname=st.getName();*/
				service+=scname+" ";
			}
			pe.setService(service);
			list1.add(pe);
		}
		return list1;
	}

	/**
	 * 供应商注册 返回是否成功
	 */
	public boolean providerRegister(Provider provider) {
		System.out.println(provider.getCity().getIndex());
		provider.setState(EnumState.STATE_UNAUTHORIZED);
		if (providerDao.save(provider)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 供应商登录
	 * 
	 * @param provider
	 * @return 登录是否成功
	 */
	public boolean providerLogin(Provider provider) {
		String username = provider.getUsername();
		String password = provider.getPassword();
		if (providerDao.findByUsername(username) != null) {
			Provider provider2 = (Provider) providerDao.findByUsername(provider.getUsername());
			if (password.equals(provider2.getPassword())) {
				// 记录provider对象
				ActionContext ctx = ActionContext.getContext();
				Map<String, Object> session;
				session = (Map<String, Object>) ctx.getSession();
				session.put("username", username);
				session.put("provider", provider2);
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}

	}

	public boolean providerSaveCatalog(int pro_id, int sc_id) {
		return providerDao.providerSaveCatalog(pro_id, sc_id);

	}

	// 供应商获取未接订单
	public List getWaitTakeOrderByProvider(int pro_id) {
		return orderDao.getWaitTakeOrderByProvider(pro_id);
	}

	// 供应商获取全部订单
	public List getAllOrderByProvider(int pro_id) {
		return orderDao.getAllOrdersByProvider(pro_id);
	}

	/**
	 * 获取未认证供应商
	 * 
	 * @return
	 */
	public List listAllUNAUTHORIZED() {
		return providerDao.listAllUNAUTHORIZED();

	}

	/**
	 * 根据id得到供应商
	 * 
	 * @param pro_id
	 */
	public void providerDetail(int pro_id) {
		Provider provider = (Provider) providerDao.findById(pro_id);
		System.out.println(provider.getPermitpath());
		City city = (City) cityDao.findById(provider.getCity().getId());
		provider.setCity(city);
		Map session = (Map) ActionContext.getContext().get("session");
		session.put("provider", provider);
	}

	// 供应商查看服务价格
	public List showServicePrice(int pro_id) {
		return providerDao.showServicePrice(pro_id);
	}

	public String checkProviderUsername(String username) {
		if (providerDao.findByUsername(username) != null)
			return "false";
		else
			return "true";
	}

	public void providerRank() 
	{
			providerDao.providerRank();
	}

	//列出被收藏的供应商
	public List<ProviderEntity> listCollectedProvider() throws Exception {
		request = ServletActionContext.getRequest();
		request.setCharacterEncoding("utf-8");
		int re_id = Integer.parseInt(request.getParameter("re_id"));		//从安卓端提交的数据获取居民id
		List<Object> list = providerDao.findCollectedProvider(re_id);
		List<ProviderEntity> list1 = new ArrayList<ProviderEntity>();
		Provider p;
		for (int i = 0; i < list.size(); i++) {
			p = (Provider) list.get(i);
			ProviderEntity pe = new ProviderEntity(p.getId(), p.getName(), p.getRank(), p.getPhone());
			if (p.getIntroduction() == null) {
				pe.setIntroduction("");
			} else {
				pe.setIntroduction(p.getIntroduction());
			}
			if (p.getPicturepath() == null) {
				pe.setPicturepath("");
			} else {
				String realpath = ServletActionContext.getServletContext().getRealPath("/permit") + "\\";
				String name = p.getPicturepath().replace(realpath, "");
				pe.setPicturepath(name);
			}
			List<Object> pscList = providerDao.getSCbyPid(p.getId());
			String service="";
			for (int j = 0; j < pscList.size(); j++) {
				Provider_servicecatalog psc = (Provider_servicecatalog) pscList.get(j);
				Servicecatalog sc = psc.getServicecatalog();
				String scname = sc.getName();
				/*Servicetype st=sc.getServicetype();
				String stname=st.getName();*/
				service+=scname+" ";
			}
			pe.setService(service);
			list1.add(pe);
		}
		return list1;
	}
	/**
	 * 注销
	 */
	public void logout(){
		ActionContext ctx = ActionContext.getContext();
		Map<String, Object> session;
		session = (Map<String, Object>) ctx.getSession();
		session.clear();
	}
}
